package com.pxml2pdf.pdf.Node;

import java.net.URL;

import org.dom4j.Element;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPTable;
import com.pxml2pdf.pdf.PageManger;
import com.pxml2pdf.utility.X2pUtility;

public abstract class BaseNode {
	protected static final int LEADING = 8;
	private static final String NONE = "none";
	private static final String CHECKED = "checked";
	private static final String DISABLED = "disabled";
	private static final float EXPANSION_Y_MAX= 4f;
	protected static final String STAR = "*";
	protected static Font FONT_STAR;
	protected static final BaseColor LINE_COLOR;
	protected static final float FONT_SIZE_DEVIATION = 0f;
	protected static final float LINE_WIDTH = 0.4f;
	static {
		FONT_STAR = new Font();
		FONT_STAR.setStyle(Font.BOLD);
		FONT_STAR.setColor(255, 0, 0);
		FONT_STAR.setFamily("Arial, Helvetica, sans-serif");

		LINE_COLOR = new BaseColor(210, 210, 210);
	}
	
	public void off_Y(float val) {
		lly += val;
		ury += val;
	}
	
	public void off_X(float val) {
		llx += val;
		urx += val;
	}

	public void expansion(float val) {
		expansion_X(val);
		expansion_Y(val);
	}

	/**
	 * @param val
	 */
	public void expansion_Y(float val) {
		lly -= val;
		ury += val;
	}

	/**
	 * @param val
	 */
	public void expansion_X(float val) {
		llx -= val;
		urx += val;
	}

	private float llx;
	private float lly;
	private float urx;
	private float ury;
	private int align;
	private float scale;
	private float expansionYByFont=0f;
	
	public float getExpansionYByFont() {
		return expansionYByFont;
	}

	public float getLlx() {
		return llx;
	}

	public void setLlx(float llx) {
		this.llx = llx;
	}

	public float getLly() {
		return lly;
	}

	public void setLly(float lly) {
		this.lly = lly;
	}

	public float getUrx() {
		return urx;
	}

	public void setUrx(float urx) {
		this.urx = urx;
	}

	public float getUry() {
		return ury;
	}

	public void setUry(float ury) {
		this.ury = ury;
	}

	public int getAlign() {
		return align;
	}

	public void setAlign(int align) {
		this.align = align;
	}

	public float getScale() {
		return scale;
	}

	public void setScale(float scale) {
		this.scale = scale;
	}

	public boolean isChecked() {
		return CHECKED.equals(value);
	}

	public boolean isDisabled() {
		return DISABLED.equals(disabled);
	}

	public boolean isHide() {
		return NONE.equals(display);
	}

	private int hmtlFontSize = 8;

	public int getHmtlFontSize() {
		return hmtlFontSize;
	}

	private String nodeType;
	private String index;
	private String id;
	private Float left;
	private Float top;
	private Float width;
	private Float height;
	private String value;
	private String fontFamily;
	private String fontSize;
	private String fontStyle;
	private String fontWeight;
	private String classes;
	private String color;
	private String textAlign;
	private String disabled;
	private String display;
	private String backgroundImage;
	private String backgroundColor;
	private String borderBottom;
	private String margin;
	private String textDecoration;
	private int pagei = 0;
	private float lly_base;
	private float ury_base;

	public String getTextDecoration() {
		return textDecoration;
	}

	public void setTextDecoration(String textDecoration) {
		this.textDecoration = textDecoration;
	}

	public String getBackgroundColor() {
		return backgroundColor;
	}

	public void setBackgroundColor(String backgroundColor) {
		this.backgroundColor = backgroundColor;
	}

	public String getBorderBottom() {
		return borderBottom;
	}

	public void setBorderBottom(String borderBottom) {
		this.borderBottom = borderBottom;
	}

	public String getMargin() {
		return margin;
	}

	public void setMargin(String margin) {
		this.margin = margin;
	}

	public String getBackgroundImage() {
		return backgroundImage;
	}

	public void setBackgroundImage(String backgroundImage) {
		this.backgroundImage = backgroundImage;
	}

	public String getDisplay() {
		return display;
	}

	public void setDisplay(String display) {
		this.display = display;
	}

	public String getDisabled() {
		return disabled;
	}

	public void setDisabled(String disabled) {
		this.disabled = disabled;
	}

	public String getNodeType() {
		return nodeType;
	}

	public void setNodeType(String nodeType) {
		this.nodeType = nodeType;
	}

	public String getIndex() {
		return index;
	}

	public void setIndex(String index) {
		this.index = index;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public Float getLeft() {
		return left;
	}

	public void setLeft(Float left) {
		this.left = left;
	}

	public Float getTop() {
		return top;
	}

	public void setTop(Float top) {
		this.top = top;
	}

	public Float getWidth() {
		return width;
	}

	public void setWidth(Float width) {
		this.width = width;
	}

	public Float getHeight() {
		return height;
	}

	public void setHeight(Float height) {
		this.height = height;
	}

	public String getValue() {
		if (value == null)
			value ="";
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public String getClasses() {
		return classes;
	}

	public void setClasses(String classes) {
		this.classes = classes;
	}

	public String getColor() {
		return color;
	}

	public void setColor(String color) {
		this.color = color;
	}

	public String getTextAlign() {
		return textAlign;
	}

	public void setTextAlign(String textAlign) {
		this.textAlign = textAlign;
	}
	
	public String getFontFamily() {
		return fontFamily;
	}

	public void setFontFamily(String fontFamily) {
		this.fontFamily = fontFamily;
	}

	public String getFontSize() {
		return fontSize;
	}

	public void setFontSize(String fontSize) {
		this.fontSize = fontSize;
	}

	public String getFontStyle() {
		return fontStyle;
	}

	public void setFontStyle(String fontStyle) {
		this.fontStyle = fontStyle;
	}

	public String getFontWeight() {
		return fontWeight;
	}

	public void setFontWeight(String fontWeight) {
		this.fontWeight = fontWeight;
	}

	public URL getIcons(String type, Boolean checked, Boolean disabled) {
		String name = type + (checked ? "-on" : "-off") + (disabled ? "-g" : "") + ".gif";
		return getIcons(name);
	}

	public URL getIcons(String name) {
		return X2pUtility.getIcons(name);
	}

	private Font getFont() {
		Font font = X2pUtility.getFont(value, fontFamily,fontSize,fontStyle,fontWeight, color, textDecoration);
		hmtlFontSize = (int) font.getSize();
		font.setSize(hmtlFontSize * scale + FONT_SIZE_DEVIATION);
		return font;
	}

	public PdfPTable getTableInfo() {
		return null;
	}

	public abstract void drawingBox(PdfContentByte cb);

	public void drawing(PdfContentByte cb) throws DocumentException {
		if (!isHide()) {
			if ((getLeft() <= 1f && getTop() <= 1f)|| getWidth()<=0f ||getHeight()<=0f) {
				// System.out.println(getValue() + " id=" + getId() +
				// "   clase=" + getClasses() + "   x=" + getDisplay()
				// + " top=" + getTop());
				return;
			}
			if (pagei > 0) {
				this.ury = ury_base + (pagei * PageManger.PAGE_HEIGHT);
				this.lly = lly_base + (pagei * PageManger.PAGE_HEIGHT);
			} else {
				lly_base = lly;
				ury_base = ury;
			}
			drawingBox(cb);
			Font font = getFont();
			checkYByFont(font);
			fillValue(cb, font);
		}
		pagei++;
	}

	private void checkYByFont(Font font) {
		//System.out.println("fontSize:"+font.getSize()+"      height:"+(ury-lly)+ "   value:"+value);
		expansionYByFont = 0f;
		float ex_y= (ury-lly)-font.getSize();
		if (ex_y<EXPANSION_Y_MAX){
			expansionYByFont = EXPANSION_Y_MAX+font.getSize()-(ury-lly);
		}
		expansion_Y(expansionYByFont);
		//System.out.println("fontSize:"+font.getSize()+"      height:"+(ury-lly)+ "   value:"+value);
	}

	/**
	 * @param cb
	 * @param ni
	 * @param pnode
	 * @param font
	 * @throws DocumentException
	 */
	protected void fillValue(PdfContentByte cb, Font font) throws DocumentException {
		Phrase phrase = new Phrase(getValue(), font);
		ColumnText ct = new ColumnText(cb);
		ct.setSimpleColumn(phrase, getLlx(), getLly(), getUrx(), getUry(), LEADING, getAlign());
		// System.out.println(getValue() + "   x=" + getLlx() + " y=" +
		// getLly());
		ct.go();
	}

	public void parse(Element node) {
		setClasses(node.attributeValue("classes"));
		setColor(node.attributeValue("color"));
		setFontFamily(node.attributeValue("fontFamily"));
		setFontSize(node.attributeValue("fontSize"));
		setFontStyle(node.attributeValue("fontStyle"));
		setFontWeight(node.attributeValue("fontWeight"));
		setHeight(X2pUtility.ps2d(node.attributeValue("height")));
		setId(node.attributeValue("id"));
		setIndex(node.attributeValue("index"));
		setLeft(X2pUtility.ps2d(node.attributeValue("left")));
		setNodeType(node.attributeValue("nodeType"));
		setTextAlign(node.attributeValue("textAlign"));
		setTop(X2pUtility.ps2d(node.attributeValue("top")));
		setValue(node.attributeValue("value"));
		setWidth(X2pUtility.ps2d(node.attributeValue("width")));
		setDisabled(node.attributeValue("disabled"));
		setDisplay(node.attributeValue("display"));
		setBackgroundImage(node.attributeValue("backgroundImage"));
		setBackgroundColor(node.attributeValue("backgroundColor"));
		setBorderBottom(node.attributeValue("borderBottom"));
		setMargin(node.attributeValue("margin"));
		setTextDecoration(node.attributeValue("textDecoration"));
	}

}
